package com.woniuxy.controller;

import com.woniuxy.entity.Address;
import com.woniuxy.entity.Community;
import com.woniuxy.entity.Picture;
import com.woniuxy.service.CommunityService;
import com.woniuxy.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/community")
public class CommunityController {
    @Autowired
    private CommunityService communityService;

    /**
     * 查询社区列表 包含 社区名称、社区简介、社区地址 入住信息
     */
    @RequestMapping("/communityList")
    public ResponseResult communityList() {
        List<Community> communityList = communityService.communityList();
        return ResponseResult.success(communityList);

    }
    @RequestMapping("/getCommunityById")
    public ResponseResult getCommunityById(@RequestParam("comId") Integer comId) {
        Community communityList = communityService.getCommunityById(comId);
        return ResponseResult.success(communityList);

    }



    /**
     * 查询社区数量  房间人数、入住人
     */
    @RequestMapping("/communityCount")
    public ResponseResult communityCount() {
        Map<String, Integer> communityCount = new HashMap<>();
        Integer comCount = communityService.count();
        Integer roomCount= communityService.roomCount();
        Integer inhabitedCount = communityService.inhabitedCount();
        communityCount.put("comCount", comCount);
        communityCount.put("roomCount", roomCount);
        communityCount.put("inhabitedCount", inhabitedCount);
        return ResponseResult.success(communityCount);
    }

    /**
     * 查询社区列表 按照平分高低顺序
     */
    @RequestMapping("/communityRecommendList")
    public ResponseResult communityRecommendList() {
        List<Community> communityList = communityService.communityRecommendList();
        return ResponseResult.success(communityList);
    }

    @RequestMapping("/communityListByAddress")
    public ResponseResult communityListByAddress(@RequestParam("address") String address) {
        System.out.println("address:" + address);
        //按照‘,’分割地址
        String[] addressArr = address.split(",");
        Address addressEntity = new Address();
        addressEntity.setMarket(addressArr[0]);
        addressEntity.setRegion(addressArr[1]);
        addressEntity.setDetails(addressArr[2]);
        List<Community> communityList = communityService.communityListByAddress(addressEntity);
       return ResponseResult.success(communityList);

    }

    /**
     * 通过comId查询社区图片
     */
    @RequestMapping("/getCommunityImg")
    public ResponseResult getCommunityImg(@RequestParam("comId") Integer comId) {
        List<Picture> pictureList = communityService.getCommunityImg(comId);
        return ResponseResult.success(pictureList);
    }

    @RequestMapping("/getCommunityAddress")
    public ResponseResult getCommunityAddress(@RequestParam("comId") Integer comId) {
        Address address = communityService.getCommunityAddress(comId);
        return ResponseResult.success(address);
    }
    @PostMapping("/notify")
    public ResponseResult checkIn(@RequestParam Map<String, Object> map) {
        //分别获取参数

        //分割body参数 存入数组
        String str = (String)map.get("body");
        String[] strArr = str.split(",");
        //获取参数
        Integer addId = Integer.parseInt(strArr[2]);
        // 定义格式器，当时间部分缺失时默认补充 00:00:00
        LocalDate checkInTime = LocalDate.parse(strArr[0], DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDate checkOutTime = LocalDate.parse(strArr[1], DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        Integer userId = Integer.parseInt(strArr[3]);
        System.out.println("userId:" + userId);
        System.out.println("addId:" + addId);
        System.out.println("checkInTime:" + checkInTime);
        System.out.println("checkOutTime:" + checkOutTime);

        communityService.checkIn(addId, checkInTime, checkOutTime, userId);

        //输出map
        System.out.println(map);
        System.out.println("--------------------------------------------------------------");
        //获取参数

        return ResponseResult.success();
    }
}
